package com.office.onlineInformation.draftBox.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.office.onlineInformation.draftBox.pojo.MessageEs;
import com.office.onlineInformation.draftBox.pojo.OaOnlineMessage;
import com.office.onlineInformation.draftBox.dao.draftBoxMapper;
import com.office.onlineInformation.draftBox.service.draftBoxService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.office.personalOffice.personalInfo.dao.IPersonalInfo;
import com.office.personalOffice.personalInfo.model.TbStaff;
import com.office.util.PageModel;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 88168
 * @since 2022-10-25
 */
@Service
public class draftBoxServiceImpl extends ServiceImpl<draftBoxMapper, OaOnlineMessage> implements draftBoxService {

    @Autowired
    public draftBoxMapper mapper;
    @Autowired
    public IPersonalInfo mapper2;

    /**
     * 添加留言
     *
     * @param onlineMessage
     * @return
     */
    public String addMessage(OaOnlineMessage onlineMessage) {
        TbStaff staff = new TbStaff();
        String[] strings = onlineMessage.getRecevierNo().split(",");
        String str="";
        for (String string : strings) {
            staff.setStaffId(string);
            TbStaff staffByCondition = mapper2.getStaffByCondition(staff);
            onlineMessage.setReceiverId(staffByCondition.getId());
            int insert = mapper.insertDraft(onlineMessage);
            if (insert==0){
                str +=""+string;
            }
        }
//        TbStaff staffByCondition = mapper2.getStaffByCondition(staff);
//        System.out.println(staffByCondition);
//        int insert = mapper.insert(onlineMessage);
        if (str.length()==0){
//        if (insert!=0){
            return "添加成功";
        }else {
            return str+"添加失败";
        }
    }

    /**
     *  删除未发送留言
     * @param informationId
     * @return
     */
    @Override
    public String deleteMessage(int informationId) {
        int delete = mapper.updateUnsend(informationId);
        if (delete!=0){
            return "删除成功";
        }else {
            return "删除失败";
        }
    }

    /**
     * 修改留言
     * @param onlineMessage
     * @return
     */
    @Override
    public String updateMessage(OaOnlineMessage onlineMessage) {
        int update = mapper.updateMessage(onlineMessage);
        if (update!=0){
            return "修改成功";
        }else {
            return "修改失败";
        }
    }
    @Autowired
    private RestHighLevelClient restHighLevelClient;
    /**
     * 查询草稿箱留言
     * @param message
     * @param pageModel
     * @return
     */
    @Override
    public PageModel<OaOnlineMessage> getList(OaOnlineMessage message, PageModel<OaOnlineMessage> pageModel) throws IOException {
        pageModel.setPageNum(pageModel.getPageNum() <= 0 ? 1 : pageModel.getPageNum());
        pageModel.setPageSize(pageModel.getPageSize() <= 0 ? 4 : pageModel.getPageSize());
        PageHelper.startPage(pageModel.getPageNum(), pageModel.getPageSize());
        List<OaOnlineMessage> list = mapper.getList(message);
        PageInfo<OaOnlineMessage> pageInfo = new PageInfo<OaOnlineMessage>(list,4);
        pageModel.setData(new ArrayList<OaOnlineMessage>(list));
        pageModel.setTotalPage(pageInfo.getPages());
        pageModel.setTotalRows(pageInfo.getTotal());
        pageModel.setNavigatepageNums(pageInfo.getNavigatepageNums());
        pageModel.setFirstPage(pageInfo.isIsFirstPage());
        pageModel.setLastPage(pageInfo.isIsLastPage());
        pageModel.setNavigateFirstPage(pageInfo.getNavigateFirstPage());
        pageModel.setNavigateLastPage(pageInfo.getNavigateLastPage());
        pageModel.setNavigatepageLength();
        return pageModel;
    }

    @Override
    public OaOnlineMessage getById(int informationId) {
        return mapper.getDraftById(informationId);
    }

}
